<?php

/*
@版本日期: 2011年4月3日
@著作权所有: 1024 intelligence ( http://www.1024i.com )

获得使用本类库的许可, 您必须保留著作权声明信息.
报告漏洞，意见或建议, 请联系 Lou Barnes(iua1024@gmail.com)
*/

class library_captcha extends library
{

	private $image;			// 画布
	private $width;			// 宽度
	private $height;		// 高度

	private $font_family;	// 字体
	private $font_size = 16;		// 大小
	private $font_color = array(0,0,0);		// 颜色

	private $bg_color = array(255,255,255);		// 背景颜色
	private $bg_transparent = true;				// 是否透明

	private $text;			// 输出的字符
	private $text_length=4;	// 输出的字符长度

	// 构造函数
	public function __construct()
	{
		$this->font_family = BONE_ROOT.DS.'libraries'.DS.'captcha'.DS.'verdana.ttf';
	}


	// 析构函数
	public function __destruct()
	{
		if($this->image) imagedestroy($this->image);
	}

	public function set_size($width, $height){ $this->width=$width; $this->height=$height; }	// 设置图片大小
	public function set_width($width){ $this->width=$width; }	// 设置图片宽度
	public function set_height($height){ $this->height=$height; }	// 设置图片高度

	public function set_font_family($font_family){ $this->font_family=$font_family; }	// 设置字体 绝对路径
	public function set_font_size($font_size){ $this->font_size=$font_size; }	// 设置字体大小
	public function set_font_color($font_color){ $this->font_color=$font_color; }	// 设置字体颜色

	public function set_bg_color($bg_color){ $this->bg_color=$bg_color; }	// 设置背景颜色
	public function set_bg_transparent($bg_transparent){ $this->bg_transparent = $bg_transparent; }	// 设置背景是否透明

	public function set_text_length($text_length){ $this->text_length = $text_length; }	// 设置字符长度

	public function draw()		// 开始画
	{
		if(!$this->width){ $this->width = floor($this->font_size*1.3)*$this->text_length+10; }
		if(!$this->height){ $this->height = $this->font_size*2; }

		$this->image = imagecreate($this->width,$this->height);
		imagecolortransparent($this->image, imagecolorallocate($this->image,$this->bg_color[0],$this->bg_color[1],$this->bg_color[2]) );

		$str='abcdefghijkmnpqrstuvwxy3456789';
		$len = strlen($str)-1;
		for($i=0; $i<$this->text_length; $i++){$this->text[] = $str[rand(0,$len)];}

		$font_color = imagecolorallocate($this->image,$this->font_color[0],$this->font_color[1],$this->font_color[2]);

		for($i=0;$i<$this->text_length;$i++)
		{
			$angle=rand(-1,1)*rand(1,20);
			imagettftext($this->image, $this->font_size, $angle, 5+$i*floor($this->font_size*1.3), floor($this->height*0.75), $font_color, $this->font_family, $this->text[$i]);
		}
	}


	public function add_point($n=100, $color=null)	//添加干扰点
	{
		if(!$color) $color = $this->font_color;
		$color = ImageColorAllocate($this->image, $color[0], $color[1], $color[2]);
		for($i=0; $i<$n; $i++){imagesetpixel($this->image, rand(0,$this->width), rand(0,$this->height), $color);}
	}


	public function add_line($n=5, $color=null)	//添加干扰线
	{
		if(!$color) $color = $this->font_color;
		$color = imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
		for($i=0; $i<$n; $i++){imageline($this->image,0,rand(0,$this->width),$this->width,rand(0,$this->height),$color);}
	}


	public function add_border($n=1, $color=null)	//添加边框
	{
		if(!$color) $color = $this->font_color;
		$color = imagecolorallocate($this->image, $color[0], $color[1], $color[2]);
		imagerectangle($this->image,0,0,$this->width-$n,$this->height-$n,$color);
	}
	

	public function output()	//输出图像
	{
		header("Content-type: image/gif");
		imagegif($this->image);
		return $this->to_string();
	}

	
	public function to_string()		//获取输出的字符
	{
		return is_array($this->text)?implode('',$this->text):'';
	}


}
?>